//
//  ViewController.m
//  LottieAnimation
//
//  Created by tianlong on 2019/4/4.
//  Copyright © 2019年 tianlong. All rights reserved.
//


#import "ViewController.h"
#import <Lottie/Lottie.h>
#import "WXAnimationManager.h"
#import "Masonry.h"
#import "PKTestTableView.h"
#import "WXPKAnimationView.h"
#import "WXNumberAnimationManager.h"
//#import "LiveNewSignViewSec.h"
#import "UIView+WXAutoLayout.h"
#import "LiveNewFollowBubbleView.h"
#import <Photos/Photos.h>
#import "LiveNewSignViewSec.h"
#import "LiveNewSignViewModelSec.h"
#import <YYLabel.h>
#import <YYKit/NSAttributedString+YYText.h>

#import "LiveNewCollectionView.h"

#define WXLocalLogDirectory @"WXLocalLogDirectory"


@interface ViewController ()<WXAnimationDelegate>

@property (strong, nonatomic) WXPKAnimationView *animateView;

@property (nonatomic, strong) PKTestTableView *testTable;
@property (strong, nonatomic) WXAnimationManager *aniManager;
@property (strong, nonatomic) UIView *contentView;
@property (weak,   nonatomic) UIButton *button;
@property (strong, nonatomic) UILabel *testLabel;
@property (strong, nonatomic) UIButton *testButton;
@property (strong, nonatomic) WXNumberAnimationManager *numManager;

@property (weak,   nonatomic) UIButton *signBtn;
@property (weak,   nonatomic) UIImageView *bindHeadView;
@property (weak,   nonatomic) UIView *bindSignView;
@property (weak,   nonatomic) UIView *signView;
@property (weak,   nonatomic) UIView *bindBgView;
@property (weak,   nonatomic) LiveNewFollowBubbleView *privateChatView;


//@property (weak,   nonatomic) UIView *bindSignView;
@property (assign, nonatomic) CGRect lastFrame;
@property (weak,   nonatomic) UIView *bindCloseBtn;
@property (weak,   nonatomic) UIButton *closeBtn;
@property (assign, nonatomic) CGRect lastCloseFrame;
@property (assign, nonatomic) int index;
@end

@implementation ViewController

static NSString *signShowJson = @"livenewsign_show";
static NSString *signWaitJson = @"livenewsign_wait";
static NSString *signDoneNoTitleJson = @"livenewsign_done";

- (WXNumberAnimationManager *)numManager {
    if (!_numManager) {
        _numManager = [[WXNumberAnimationManager alloc] init];
    }
    return _numManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.contentView = [[UIView alloc] init];
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.offset(0);
    }];
    [self configAnimationTest];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}


- (WXAnimationManager *)aniManager {
    if (!_aniManager) {
        _aniManager = [[WXAnimationManager alloc] init];
    }
    return _aniManager;
}

- (void)configAnimationTest {
    NSMutableArray *dataM = [NSMutableArray array];
    
    {
        PKTestModel *stopAllAnimations = [[PKTestModel alloc] initWithTitle:@"停止所有动画" action:@selector(stopAllAnimations) target:self];
        [dataM addObject:stopAllAnimations];
    }
    {
        PKTestModel *dacall = [[PKTestModel alloc] initWithTitle:@"dacall" action:@selector(dacall) target:self];
        [dataM addObject:dacall];
    }
    self.testTable = [[PKTestTableView alloc] initWithDataSource:dataM];
    [self.view addSubview:self.testTable];
    self.testTable.alpha = 0.4;
    [self.testTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.right.offset(44);
        make.width.offset(200);
        make.height.offset(600);
    }];
}

- (void)stopAllAnimations {
    [self.aniManager stopAllAnimations];
    [self.privateChatView removeFromSuperview];
    [self.privateChatView dismissWithAimate:YES];
    self.privateChatView = nil;
}

- (void)dacall {
    __weak typeof(self)selfWeak = self;
    
//    NSString *json = @"data";
//    if ([self.aniManager getAnimationByIdentifier:json]) {
//        return;
//    }
    CGFloat aniW = (1000/2) *self.view.bigClassScale;
    CGFloat aniH = (700/2) *self.view.bigClassScale;
    CGFloat fontSize = 32 *self.view.bigClassScale;
    if (IS_IPAD) {
        fontSize = 35;
    }
    
//    NSString *lottieDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
//    lottieDir = [lottieDir stringByAppendingPathComponent:@"WXLive/Lottie/Praise/"];
//
//    NSArray *lottieDirArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:lottieDir error:nil];//路径下所有动画
//    NSString *aniJson = nil;
//    if (lottieDirArr.count==1) {
//        aniJson = lottieDirArr.firstObject;
//    } else {
//        if (lottieDirArr.count>1) {
//            NSInteger lottieRandom = arc4random_uniform(lottieDirArr.count);
//            aniJson = [lottieDirArr objectAtIndex:lottieRandom];
//        }
//    }
//    NSLog(@"lottie -- 找到本地的动画json名称 = %@",aniJson);
//
//    if (aniJson.length>0) {
//        NSString *fileName = [lottieDir stringByAppendingPathComponent:aniJson];
//        fileName  = [fileName stringByAppendingPathComponent:@"data.json"];
//        self.index++;
//        NSString *idFier = [NSString stringWithFormat:@"%@_%d",fileName,self.index];
//        [self.aniManager playWithFilePath:fileName speed:1 repeatCount:0 identifier:idFier layout:^(WXAnimationView *aniView) {
//            [selfWeak.contentView addSubview:aniView];
//            aniView.userInteractionEnabled = NO;
//            aniView.backgroundImageView.hidden = NO;
//            aniView.backgroundImageView.backgroundColor = [UIColor colorWithHexValue:0x000000 alpha:0.3];
//            [aniView mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.left.bottom.right.offset(0);
//            }];
//            [aniView.playView mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.center.equalTo(selfWeak.contentView);
//                make.width.offset(aniW);
//                make.height.offset(aniH);
//            }];
//            UILabel *textLabel = [[UILabel alloc] init];
//            textLabel.textAlignment = NSTextAlignmentCenter;
//            textLabel.text = @"恭喜了不起的大头答题正确，再接再接，加油啊1恭喜了不起的大头答题正确，再接再接，加油啊1";
//            textLabel.numberOfLines = 0;
//            textLabel.font = [UIFont fontPingFangSCWithSize:fontSize];
//            textLabel.textColor = [UIColor colorWithHexValue:0xFFFFFF];
//            textLabel.backgroundColor = [UIColor redColor];
//            [aniView bindingUIView:textLabel forKeyPath:@"标题.png" imageName:@"img_0.png"];
//        } autoPlay:YES completion:^(BOOL finished) {
//            [selfWeak.aniManager stopAniamtionWithIdentifier:idFier];
//        }];
//    }
//
//    return;
//
    [self.aniManager playWithJson:@"1" speed:1 repeatCount:0 identifier:@"" layout:^(WXAnimationView *aniView) {
        [selfWeak.contentView addSubview:aniView];
        aniView.userInteractionEnabled = NO;
        aniView.backgroundImageView.hidden = NO;
        aniView.backgroundImageView.backgroundColor = [UIColor colorWithHexValue:0x000000 alpha:0.3];
        [aniView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.offset(0);
        }];
        [aniView.playView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(selfWeak.contentView);
            make.width.offset(aniW);
            make.height.offset(aniH);
        }];
//        UILabel *textLabel = [[UILabel alloc] init];
//        textLabel.textAlignment = NSTextAlignmentCenter;
//        textLabel.text = @"dddddd恭喜了不起的大头答题正确，再接再接，加油啊1恭喜了不起的大头答题正确，再接再接，加油啊1";
//        textLabel.numberOfLines = 0;
//        textLabel.font = [UIFont fontPingFangSCWithSize:fontSize];
//        textLabel.textColor = [UIColor colorWithHexValue:0xFFFFFF];
//        textLabel.backgroundColor = [UIColor redColor];
//        [aniView bindingUIView:textLabel forKeyPath:@"标题.png" imageName:@"img_0.png"];
        
    } autoPlay:YES completion:^(BOOL finished) {
        [selfWeak.aniManager stopAniamtionWithIdentifier:@""];
    }];
    
}

- (void)animationViewDidPlay:(WXAnimationView *)animationView jsonName:(NSString *)jsonName {
}

- (void)clickedSignBtn:(UIButton *)btn {
    NSLog(@"点击了签到按钮");
}
@end
